package com.jdk.learn.exam.gui;

import org.junit.Test;

import javax.swing.*;
import java.awt.*;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by ricky
 * on 2018/2/27.
 * <p>
 * 布局管理器：
 * FlowLayout:流式布局,将组件逐个放置在容器中的一行。一行放满后另起新行。
 * BorderLayout:是顶层容器的默认布局，根据方位进行布局，东南西北中
 * GridLayout：网格布局。将容器划分为若干行列的网格，每个组件占据一格。
 * CardLayout：卡片布局，类似选项卡式布局，每次只显示一个卡片。
 * BoxLayout：水平或者垂直布局。将组件按水平或者垂直排成一行/列。
 * 空布局:通过setBounds进行定位。
 */
public class BorderDemo {

    /**
     * 流式布局
     * 按钮水平一行显示
     */
    @Test
    public void FlowDemo() {
        JFrame frame = GUIUtils.getDefaultFrame("流式布局");
        JPanel panel = GUIUtils.getDefaultJPanel(new FlowLayout());

        JButton button1 = new JButton("按钮1");
        JButton button2 = new JButton("按钮2");
        JButton button3 = new JButton("按钮3");
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        frame.add(panel);
        frame.pack();
        GUIUtils.Sleep();
    }

    /**
     * Border布局
     * 按钮垂直显示一列
     */
    @Test
    public void BorderDemo() {
        JFrame frame = GUIUtils.getDefaultFrame("Border布局");
        JPanel panel = GUIUtils.getDefaultJPanel(new BorderLayout());

        JButton button1 = new JButton("按钮1");
        JButton button2 = new JButton("按钮2");
        JButton button3 = new JButton("按钮3");
        panel.add(button1, BorderLayout.NORTH);
        panel.add(button2, BorderLayout.CENTER);
        panel.add(button3, BorderLayout.SOUTH);
        frame.add(panel);
        frame.pack();
        GUIUtils.Sleep();
    }

    /**
     * Grid布局
     * 四个按钮
     * 一行2个
     * 共两行
     */
    @Test
    public void GridDemo() {
        JFrame frame = GUIUtils.getDefaultFrame("Grid布局");
        JPanel panel = GUIUtils.getDefaultJPanel(new GridLayout(2, 2));

        JButton button1 = new JButton("按钮1");
        JButton button2 = new JButton("按钮2");
        JButton button3 = new JButton("按钮3");
        JButton button4 = new JButton("按钮4");
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);
        frame.add(panel);
        frame.pack();
        GUIUtils.Sleep();
    }

    /**
     * Card布局
     *四个面板 鼠标点击切换
     */
    @Test
    public void CardDemo() {
        JFrame frame = GUIUtils.getDefaultFrame("Card布局");

        CardLayout card = new CardLayout();
        Container panel = frame.getContentPane();
        panel.setLayout(card);
        MouseListener mouseListener = new MouseListener(panel, card);
        JPanel panel1 = new JPanel();
        panel1.add(new JLabel("111#####"));
        panel1.addMouseListener(mouseListener);
        JPanel panel2 = new JPanel();
        panel2.add(new JLabel("########222"));
        panel2.addMouseListener(mouseListener);

        JPanel panel3 = new JPanel();
        panel3.add(new JLabel("333#"));
        panel3.addMouseListener(mouseListener);

        JPanel panel4 = new JPanel();
        panel4.add(new JLabel("####444"));
        panel4.addMouseListener(mouseListener);

        panel.add(panel1, "1");
        panel.add(panel2, "2");
        panel.add(panel3, "3");
        panel.add(panel4, "4");
        card.show(panel,"1");
//        frame.pack();
        GUIUtils.Sleep();
    }


    /**
     * Box布局
     * 水平4个按钮
     * 垂直四个按钮
     */
    @Test
    public void BoxDemo() {
        JFrame frame = GUIUtils.getDefaultFrame("Box布局");
        Container container = frame.getContentPane();
        JPanel ph = new JPanel();//垂直方向
        ph.setLayout(new BoxLayout(ph,BoxLayout.Y_AXIS));//垂直方向
        JButton button1 = new JButton("按钮1");
        JButton button2 = new JButton("按钮2");
        JButton button3 = new JButton("按钮3");
        JButton button4 = new JButton("按钮4");
        ph.add(button1);
        ph.add(button2);
        ph.add(button3);
        ph.add(button4);

        JPanel pv = new JPanel();//水平方向
        pv.setLayout(new BoxLayout(pv,BoxLayout.X_AXIS));//水平方向
        JButton buttonX1 = new JButton("按钮X1");
        JButton buttonX2 = new JButton("按钮X2");
        JButton buttonX3 = new JButton("按钮X3");
        JButton buttonX4 = new JButton("按钮X4");
        pv.add(buttonX1);
        pv.add(buttonX2);
        pv.add(buttonX3);
        pv.add(buttonX4);


        container.add(ph,BorderLayout.SOUTH);
        container.add(pv,BorderLayout.NORTH);
        frame.pack();
        GUIUtils.Sleep();
    }
    /**
     * Box布局2
     * 不可见组件
     *
     */
    @Test
    public void BoxDemo2() {
        JFrame frame = GUIUtils.getDefaultFrame("Box布局2");
        Container container = frame.getContentPane();
        container.setLayout(new GridLayout(3,1));

        Box b1=Box.createHorizontalBox();//水平元素
        b1.add(new JLabel("第一行:  "));
        b1.add(Box.createHorizontalStrut(200));//不可见组件 进行补位
        b1.add(new JButton("button1"));
        container.add(b1);

        Box b2=Box.createHorizontalBox();//水平元素
        b2.add(new JLabel("第2行:  "));
        b2.add(Box.createHorizontalGlue());
        b2.add(new JButton("button2"));
        container.add(b2);

        Box b3=Box.createHorizontalBox();//水平元素
        b3.add(new JLabel("第3行:  "));
        b3.add(Box.createRigidArea(new Dimension(100,100)));
        b3.add(new JButton("button3"));
        container.add(b3);

        frame.pack();
        GUIUtils.Sleep();
    }

}

class MouseListener extends MouseAdapter {
    Container panel=null;
    CardLayout card=null;

    public MouseListener(Container panel,CardLayout card) {
        this.panel = panel;
        this.card = card;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        card.next(panel);
    }
}
